Blockly.Python['paho_mqtt_package'] = function(block) {
    //return "import RPi.GPIO as GPIO \nGPIO.setmode(GPIO.BOARD) \nGPIO.setwarnings(False) \n"
    return "import paho.mqtt.client as mqtt \n";
};

Blockly.Python['paho_mqtt_init'] = function(block) {
    var client = Blockly.Python.valueToCode(block, 'CLIENT', Blockly.Python.ORDER_ATOMIC);
    var session = Blockly.Python.valueToCode(block, 'SESSION', Blockly.Python.ORDER_ATOMIC);

    var code = "mqtt.Client()";
    if(client=='None' || client.length==0){
        code = "mqtt.Client(None, True)";
    }else{
        code = "mqtt.Client(" + client + ", " + session + ")";
    }

    return [code, Blockly.Python.ORDER_ATOMIC];
};

Blockly.Python['paho_mqtt_username_password'] = function(block) {
    var client = Blockly.Python.valueToCode(block, 'CLIENT', Blockly.Python.ORDER_ATOMIC);
    var username = Blockly.Python.valueToCode(block, 'USERNAME', Blockly.Python.ORDER_ATOMIC) || "";
    var password = Blockly.Python.valueToCode(block, 'PASSWORD', Blockly.Python.ORDER_ATOMIC) || "";
    return client + '.username_pw_set(' + username + ', ' + password + ') \n';
};

Blockly.Python['paho_mqtt_on_connect'] = function(block) {
    var client = Blockly.Python.valueToCode(block, 'CLIENT', Blockly.Python.ORDER_ATOMIC);
    var success = Blockly.Python.statementToCode(block, 'SUCCESS_STATEMENT');
    var failure = Blockly.Python.statementToCode(block, 'FAILURE_STATEMENT');

    var codes = [];
    codes.push("");
    codes.push("");
    codes.push("# 当连接到MQTT服务器成功回调");
    codes.push('def mqtt_on_connect_success(c):');
    if(success==''){
        codes.push('  pass');
    }else{
        codes.push(success);
    }
    codes.push("");
    codes.push("");
    codes.push("# 当连接到MQTT服务器失败时回调");
    codes.push('def mqtt_on_connect_failure():');
    if(failure==''){
        codes.push('  pass');
    }else{
        codes.push(failure);
    }
    codes.push("");
    codes.push("");
    codes.push("# 当连接到MQTT服务器时回调");
    codes.push('def mqtt_on_connect(c, ud, f, rc):');
    codes.push('  if rc==0:')
    codes.push('    mqtt_on_connect_success(c)');
    codes.push('  else:');
    codes.push('    mqtt_on_connect_failure()');
    codes.push('');
    codes.push('');
    codes.push(client + '.on_connect = mqtt_on_connect');
    codes.push('');

    return codes.join(' \n');
};

Blockly.Python['paho_mqtt_on_message'] = function(block) {
    var client = Blockly.Python.valueToCode(block, 'CLIENT', Blockly.Python.ORDER_ATOMIC);
    var statement = Blockly.Python.statementToCode(block, 'STATEMENT');

    var codes = [];
    codes.push("");
    codes.push("");
    codes.push('# 当接收到MQTT服务器发送过来的数据时回调');
    codes.push('def mqtt_on_message(c, ud, m):');
    if(statement=='') {
        codes.push('  pass');
    }else{
        codes.push(statement);
    }
    codes.push('');
    codes.push('');
    codes.push(client + '.on_message = mqtt_on_message');
    codes.push('');

    return codes.join(' \n');
};


Blockly.Python['paho_mqtt_subscribe'] = function(block) {
    var client = Blockly.Python.valueToCode(block, 'CLIENT', Blockly.Python.ORDER_ATOMIC);
    var topic = Blockly.Python.valueToCode(block, 'TOPIC', Blockly.Python.ORDER_ATOMIC);
    var qos = parseInt(block.getFieldValue('QOS')) || 0;

    return client + '.subscribe(' + topic + ', ' + qos + ') \n';
};

Blockly.Python['paho_mqtt_publish'] = function(block) {
    var client = Blockly.Python.valueToCode(block, 'CLIENT', Blockly.Python.ORDER_ATOMIC);
    var topic = Blockly.Python.valueToCode(block, 'TOPIC', Blockly.Python.ORDER_ATOMIC);
    var qos = parseInt(block.getFieldValue('QOS')) || 0;
    var payload = Blockly.Python.valueToCode(block, 'PAYLOAD', Blockly.Python.ORDER_ATOMIC);

    return client + '.publish(' + topic + ', payload=' + payload + ', qos=' + qos + ', retain=False) \n';
};

Blockly.Python['paho_mqtt_qos'] = function(block) {
    var qos = parseInt(block.getFieldValue('QOS')) || 0;
    return [qos, Blockly.Python.ORDER_ATOMIC];
};

Blockly.Python['paho_mqtt_message_topic'] = function(block) {
    return ['m.topic', Blockly.Python.ORDER_ATOMIC];
};

Blockly.Python['paho_mqtt_message_payload'] = function(block) {
    return ['str(m.payload, encoding="utf-8")', Blockly.Python.ORDER_ATOMIC];
};

Blockly.Python['paho_mqtt_message_qos'] = function(block) {
    return ['m.qos', Blockly.Python.ORDER_ATOMIC];
};

Blockly.Python['paho_mqtt_connect'] = function(block) {
    var client = Blockly.Python.valueToCode(block, 'CLIENT', Blockly.Python.ORDER_ATOMIC);
    var server = Blockly.Python.valueToCode(block, 'SERVER', Blockly.Python.ORDER_ATOMIC) || "iot.eclipse.org";
    var port = parseInt(Blockly.Python.valueToCode(block, 'PORT', Blockly.Python.ORDER_ATOMIC)) || 1883;
    var keepAlive = parseInt(Blockly.Python.valueToCode(block, 'KEEP_ALIVE', Blockly.Python.ORDER_ATOMIC)) || 60;
    var blocked = block.getFieldValue('BLOCKED')=='TRUE';

    var codes = [];
    codes.push(client + '.connect(' + server + ', ' + port + ', ' + keepAlive + ') ');
    if(blocked){
        codes.push(client + '.loop_forever() ');
    }else{
        codes.push(client + '.loop_start() ');
    }
    codes.push('');

    return codes.join(' \n');
};

Blockly.Python['paho_mqtt_disconnect'] = function(block) {
    var client = Blockly.Python.valueToCode(block, 'CLIENT', Blockly.Python.ORDER_ATOMIC);
    return client + '.disconnect() \n';
};

Blockly.Python['paho_mqtt_loop_stop'] = function(block) {
    var client = Blockly.Python.valueToCode(block, 'CLIENT', Blockly.Python.ORDER_ATOMIC);
    return client + '.loop_stop() \n';
};


